package com.mg.controller;

import com.github.pagehelper.PageInfo;
import com.mg.common.Constants;
import com.mg.dto.ContractDto;
import com.mg.dto.SimpleResult;
import com.mg.model.Contract;
import com.mg.model.MachineType;
import com.mg.model.PurchaseSale;
import com.mg.model.User;
import com.mg.service.ContractService;
import com.mg.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by mg1213 on 2018/3/18.
 */
@Controller
@RequestMapping("/contract")
public class ContractController {
    @Autowired
    private ContractService contractService;

    @RequestMapping(value = "/admin/getAllMachineTypes")
    @ResponseBody
    public List<MachineType> getAllMachineTypes() {
        return contractService.getAllMachineTypes();
    }
    @RequestMapping(value = "/admin/selectPageInfo")
    @ResponseBody
    public PageInfo<ContractDto> selectPageInfo(ContractDto contractDto, int limit, int start) {
        return contractService.selectPageInfo(contractDto, start, limit);
    }
    @RequestMapping(value = "/admin/insertContract")
    @ResponseBody
    public SimpleResult insertContract(Contract contract) {
        return contractService.insertContract(contract);
    }
    @RequestMapping(value = "/admin/updateContract")
    @ResponseBody
    public SimpleResult updateContract(Contract contract) {
        return contractService.updateContract(contract);
    }
    @RequestMapping(value = "/admin/deleteContract")
    @ResponseBody
    public SimpleResult deleteContract(String ids) {
        return contractService.deleteContract(ids);
    }
    @RequestMapping(value = "/user/buyContract")
    @ResponseBody
    public SimpleResult buyContract(BigDecimal num, Integer contractId, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Constants.USER_INFO);
        return contractService.buyContract(user.getId(),num,contractId);
    }

    @RequestMapping(value = "/user/findContractPage")
    @ResponseBody
    public PageInfo<Map<String,Object>> findContractPage(HttpServletRequest request, PurchaseSale purchaseSale, int limit, int start) {
//        if(StringUtils.isEmpty(request.getSession().getAttribute(Constants.USER_INFO))){
//            return null;
//        }

        User user = (User) request.getSession().getAttribute(Constants.USER_INFO);
        Map<String,Object> paramMap = JsonUtils.toMap(purchaseSale);
//        paramMap.put("userId",user.getId());
        return contractService.findContractPage(paramMap, start, limit);
    }
}
